package models

import (
	"fmt"
	ck_orm "github.com/clakeboy/ck-orm"
	"github.com/clakeboy/golib/utils"
)

//表字段
type Field struct {
	Name    string      `json:"name"`
	Type    string      `json:"type"`
	Default interface{} `json:"dflt_value"`
	Cid     int         `json:"cid"`
	NotNull int         `json:"notnull"`
	IsPk    int         `json:"pk"`
}

//字段MAP
type MapFields = map[string]*Field

func GetTableFields(db *ck_orm.DBA, tableName string) (MapFields, error) {
	result, err := db.Query(fmt.Sprintf("PRAGMA table_info('%s')", tableName))
	if err != nil {
		return nil, err
	}
	fields := MapFields{}
	for _, v := range result {
		field := &Field{}
		_ = utils.Map2Struct(v, field)
		fields[v["name"].(string)] = field
		if field.IsPk == 1 {
			fields["PK"] = field
		}
	}
	return fields, nil
}
